//
//  BaseTableViewController.m
//  cityStar
//
//  Created by wangjia on 15/3/28.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "BaseTableViewController.h"
#import "AppDelegate.h"
#import "HTDefine.h"

@interface BaseTableViewController ()

@end

@implementation BaseTableViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    //为了解决在自定义NavBar以后，ios7手势返回失效的问题，
    [self.navigationController.interactivePopGestureRecognizer setDelegate:nil];
    [self initNavBar];
}

- (void)initNavBar{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >=7.0) {
        self.navigationController.navigationBar.translucent = NO; //禁止半透明毛玻璃效果
        self.navigationController.navigationBar.tintColor = kWhiteNavTitleColor; //返回按钮颜色
        self.navigationController.navigationBar.barTintColor = [UIColor whiteColor]; //bar背景颜色
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:kWhiteNavTitleColor,NSFontAttributeName:[UIFont boldSystemFontOfSize:18]}; //标题颜色
        self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    }else{
        [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]]; //bar背景颜色
        self.navigationController.navigationBar.translucent = NO;
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:kWhiteNavTitleColor,NSFontAttributeName:[UIFont boldSystemFontOfSize:18]};
        self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    }
}

-(void)setNavigationColor:(UIColor *)color{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >=7.0) {
        self.navigationController.navigationBar.translucent = NO; //禁止半透明毛玻璃效果
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor]; //返回按钮颜色
        self.navigationController.navigationBar.barTintColor = color; //bar背景颜色
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:18]}; //标题颜色
        self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    }else{
        [self.navigationController.navigationBar setTintColor:color]; //bar背景颜色
        self.navigationController.navigationBar.translucent = NO;
        self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 数据相关

- (PersonalDetail *)currentUser
{
    NSData *userData = [KStandUserDefaults objectForKey:KUserDefaultKey_PersonalDetail];
    PersonalDetail *user = [NSKeyedUnarchiver unarchiveObjectWithData:userData];
    return user;
}

- (void)changeCurrentUser:(PersonalDetail *)currentUser
{
    NSData *userData = [NSKeyedArchiver archivedDataWithRootObject:currentUser];
    [KStandUserDefaults setObject:userData forKey:KUserDefaultKey_PersonalDetail];
    [KStandUserDefaults synchronize];
}

#pragma mark -导航栏相关
- (void)addBackButton
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 90, 30)];
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setFrame:CGRectMake(-30, 0, 90, 30)];
    //    [leftButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [leftButton setTitle:@"返回" forState:UIControlStateNormal];
    leftButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [leftButton setTitleColor:[UIColor colorWithWhite:0.200 alpha:1.000] forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor colorWithRed:0.991 green:0.532 blue:0.303 alpha:1.000] forState:UIControlStateHighlighted];
    [leftButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [leftButton setImage:[UIImage imageNamed:@"back_touch"] forState:UIControlStateHighlighted];
    
    //    UIImageView *imagebackView = [[UIImageView alloc]initWithFrame:CGRectMake(-10, 0, 30, 30)];
    //    imagebackView.image = [UIImage imageNamed:@"back"];
    //    [view addSubview:imagebackView];
    [view addSubview:leftButton];
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    self.navigationItem.leftBarButtonItem = barButtonItem;
    
}

-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)addLeftButtonWithImage:(UIImage *)image actionBlock:(void(^)(void))block
{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setFrame:CGRectMake(0, 0, 40, 20)];
    [leftButton setImage:image forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(leftBar_Touched:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.leftBarActionBlock = block;
    self.navigationItem.leftBarButtonItem = barButtonItem;
}

- (void)addLeftButtonWithTitle:(NSString *)title actionBlock:(void(^)(void))block
{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setFrame:CGRectMake(0, 0, 60, 20)];
    leftButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [leftButton setTitle:title forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(leftBar_Touched:) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.leftBarActionBlock = block;
    self.navigationItem.leftBarButtonItem = barButtonItem;
}

- (void)leftBar_Touched:(id)sender
{
    if (self.leftBarActionBlock) {
        self.leftBarActionBlock();
    }
}

- (void)addRightButtonWithImage:(UIImage *)image actionBlock:(void(^)(void))block
{
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setFrame:CGRectMake(0, 0, 20, 20)];
    [rightButton setImage:image forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(rightBar_Touched:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.rightBarActionBlock = block;
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}

- (void)addRightButtonWithTitle:(NSString *)title actionBlock:(void(^)(void))block
{
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setFrame:CGRectMake(0, 0, 60, 20)];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [rightButton setTitle:title forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor colorWithRed:0.991 green:0.532 blue:0.303 alpha:1.000] forState:UIControlStateHighlighted];
    [rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(rightBar_Touched:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.rightBarActionBlock = block;
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}

- (void)rightBar_Touched:(id)sender
{
    if (self.rightBarActionBlock) {
        self.rightBarActionBlock();
    }
}

- (void)setUpNavgationBar
{
    NSAssert(NO, @"需要子类重载");
}

#pragma mark - 网络相关

@end
